package com.cuz.hot100;

import java.util.Arrays;

public class Q26RemoveDuplicatesSortedArray {

    public static void main(String[] args) {
        int[] ints = {0, 1, 1, 2, 2, 5, 5, 6};
        int i = removeDuplicates1(ints);
        System.out.println(Arrays.toString(ints));
        System.out.println(i);
    }

    public static int removeDuplicates2(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int slow = 1;
        int fast = 1;
        while (fast < nums.length) {
            if (nums[fast] != nums[slow - 1]) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }
        return slow;
    }

    public static int removeDuplicates1(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int res = 1;
        int curMax = nums[0];
        for (int index = 1; index < nums.length; index++) {
            if (nums[index] <= nums[index - 1]) {
                for (int find = index + 1; find < nums.length; find++) {
                    if (nums[find] > curMax) {
                        nums[index] = nums[find];
                        res++;
                        break;
                    }
                    if (find == nums.length - 1) {
                        return res;
                    }
                }
            } else {
                res++;
            }
            curMax = Math.max(curMax, nums[index]);
        }
        return res;
    }
}
